﻿Imports System.Data.SqlClient
Imports System.Data

Partial Public Class WebUserControl
    Inherits System.Web.UI.UserControl
    Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Conexion1").ConnectionString)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            PopulateMenu()
        End If

    End Sub


    Sub PopulateMenu()
        Dim dst As System.Data.DataSet = GetMenuData()
        For Each masterRow As System.Data.DataRow In dst.Tables("MENU1").Rows()
            Dim masterItem As New MenuItem(masterRow("Inner_Html").ToString(), "", "", masterRow("ONCLICK").ToString())
            Menu1.Items.Add(masterItem)
            For Each childRow As System.Data.DataRow In masterRow.GetChildRows("Children")
                Dim childItem As New MenuItem(childRow("Inner_Html").ToString(), "", "", childRow("ONCLICK").ToString())
                masterItem.ChildItems.Add(childItem)
                For Each childRow1 As System.Data.DataRow In childRow.GetChildRows("SubChildren")
                    Dim childItem1 As New MenuItem(childRow1("Inner_Html").ToString(), "", "", childRow1("ONCLICK").ToString())
                    childItem.ChildItems.Add(childItem1)
                Next
            Next
        Next

    End Sub

    Function GetMenuData() As System.Data.DataSet
        'Dim con As New System.Data.SqlClient.SqlConnection(connectionString)
        Dim dadMenu1 As New  _
          System.Data.SqlClient.SqlDataAdapter("SELECT * FROM MENU where level=0 and id_rol ='" + Session.Item("Rol") + "' and system_id ='" + Session.Item("SistemaID") + "'", cn)
        Dim dadMenu2 As New  _
        System.Data.SqlClient.SqlDataAdapter("SELECT * FROM MENU where level=1 and id_rol = '" + Session.Item("Rol") + "' and system_id ='" + Session.Item("SistemaID") + "'", cn)
        Dim dadMenu3 As New  _
       System.Data.SqlClient.SqlDataAdapter("SELECT * FROM MENU where level=2 and id_rol = '" + Session.Item("Rol") + "'and system_id ='" + Session.Item("SistemaID") + "'", cn)
        Dim dst As New System.Data.DataSet()
        dadMenu1.Fill(dst, "MENU1")
        dadMenu2.Fill(dst, "MENU2")
        dadMenu3.Fill(dst, "MENU3")
        dst.Relations.Add("Children", _
            dst.Tables("MENU1").Columns("ID"), _
            dst.Tables("MENU2").Columns("Parent_ID"))
        dst.Relations.Add("SubChildren", _
            dst.Tables("MENU2").Columns("ID"), _
            dst.Tables("MENU3").Columns("Parent_ID"))
        cn.Close()
        Return dst
    End Function


End Class